var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), a = e(require("../../@babel/runtime/regenerator")), r = require("../../@babel/runtime/helpers/asyncToGenerator"), s = e(require("../../http/order")), n = e(require("../../http/address")), d = e(require("../../http/system")), o = require("../../utils/tool"), i = e(require("../../behavior/orderBehavior")), c = new s.default(), u = new n.default(), f = new d.default();

Page({
    behaviors: [ i.default ],
    data: {
        orderId: "",
        details: {},
        confirmFlag: !1,
        remainingDays: "",
        leftTime: ""
    },
    onLoad: function(e) {
        this.setData({
            orderId: e.orderId
        });
    },
    toGoodsDetails: function(e) {
        var t = e.currentTarget.dataset.productid, a = e.currentTarget.dataset.producttype;
        t && (0 == a ? wx.navigateTo({
            url: "/pages/direct-buy-details/direct-buy-details?productId=".concat(t)
        }) : wx.navigateTo({
            url: "/pages/series/series?productId=".concat(t)
        }));
    },
    toOrderPick: function() {
        wx.redirectTo({
            url: "/pages/order_pick/order_pick?orderId=".concat(this.data.orderId)
        });
    },
    showConfirm: function() {
        this.setData({
            confirmFlag: !0
        });
    },
    showLogistics: function() {
        wx.navigateTo({
            url: "/pages/logistics-details/logistics-details?orderId=" + this.data.orderId
        });
    },
    showFreight: function() {
        this.selectComponent("#freight").showDialog();
    },
    confirmGet: function() {
        var e = this;
        return r(a.default.mark(function t() {
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, wx.showLoading({
                        title: "加载中",
                        mask: !0
                    }), t.next = 4, c.confirmReceipt(e.data.details.orderId);

                  case 4:
                    return (0, o.toastToPage)("确认收货成功"), t.next = 7, e.getDetail();

                  case 7:
                    e._sendRefresh(), wx.hideLoading(), t.next = 14;
                    break;

                  case 11:
                    t.prev = 11, t.t0 = t.catch(0), wx.hideLoading();

                  case 14:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 11 ] ]);
        }))();
    },
    showPostage: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, s;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return r = e.data.orderId, t.next = 3, c.postage({
                        orderIds: r
                    });

                  case 3:
                    s = t.sent, e.setData({
                        needPost: s.data.openStatus
                    }), 1 == s.data.openStatus ? (e.getPostInformation(), e.setData({
                        show: !0
                    })) : (e.setData({
                        addPickParams: {
                            addressId: e.data.defaultAddress.addressId,
                            omsOrderList: e.data.orderId.split(","),
                            shippingCouponHistoryId: "",
                            type: 2
                        }
                    }), e.addPick(), (0, o.toastToPage)("申请发货成功", "/pages/pick_records/pick_records"));

                  case 6:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    getDefaultAddress: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, u.getDefaultAddress();

                  case 2:
                    r = t.sent, e.setData({
                        defaultAddress: r.data
                    });

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    getPostInformation: function() {
        var e = this;
        return r(a.default.mark(function r() {
            var s, n, d, o, i, u;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return s = {
                        addressId: e.data.defaultAddress.addressId,
                        omsOrderList: e.data.orderId.split(",")
                    }, a.next = 3, c.postInformation(s);

                  case 3:
                    n = a.sent, d = [], o = t(n.data.omsOrderShippingPriceVOList);
                    try {
                        for (o.s(); !(i = o.n()).done; ) u = i.value, d.push({
                            id: u.shippingTemplatePriceId,
                            money: u.farShippingMoney,
                            name: u.expressCompanyName,
                            type: 0
                        });
                    } catch (e) {
                        o.e(e);
                    } finally {
                        o.f();
                    }
                    n.data.amsCouponHistoryVO && d.push({
                        id: n.data.amsCouponHistoryVO.couponHistoryId,
                        money: "",
                        name: "免邮券",
                        type: 1
                    }), e.setData({
                        postInformation: d,
                        addPickParams: {
                            addressId: e.data.defaultAddress.addressId,
                            omsOrderList: e.data.orderId.split(","),
                            shippingCouponHistoryId: d[0].id,
                            type: d[0].type
                        },
                        freeData: {
                            freeNum: n.data.freeNum,
                            freePrice: n.data.freePrice
                        }
                    });

                  case 9:
                  case "end":
                    return a.stop();
                }
            }, r);
        }))();
    },
    setPost: function(e) {
        this.setData({
            addPickParams: {
                addressId: this.data.defaultAddress.addressId,
                omsOrderList: this.data.orderId.split(","),
                shippingCouponHistoryId: e.detail.id,
                type: e.detail.type
            }
        });
    },
    addPick: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, s;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, c.addPick(e.data.addPickParams);

                  case 2:
                    r = t.sent, e.data.needPost && (s = r.data.mergeOrderId, e.startPay(s));

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    startPay: function(e) {
        return r(a.default.mark(function t() {
            var r, s, n, d, i;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, (0, o.promising)(wx.login)();

                  case 2:
                    return r = t.sent, s = r.code, t.next = 6, c.getPostWxPayInfo(s, e);

                  case 6:
                    n = t.sent, d = JSON.parse(n.data), i = {
                        timeStamp: d.timeStamp,
                        nonceStr: d.nonceStr,
                        package: d.package,
                        paySign: d.paySign,
                        signType: d.signType,
                        success: function(e) {
                            console.log(e), (0, o.toastToPage)("申请发货成功", "/pages/pick_records/pick_records");
                        },
                        fail: function(t) {
                            console.log(t), c.cancelPick(e);
                        }
                    }, wx.requestPayment(i);

                  case 10:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    getDetail: function() {
        var e = this;
        return r(a.default.mark(function r() {
            var s, n, d, i, u, p, l, g, m;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return a.next = 2, c.getOrderDetail(e.data.orderId);

                  case 2:
                    s = a.sent, n = !1, d = t(s.data.orderItemVoList), a.prev = 5, d.s();

                  case 7:
                    if ((i = d.n()).done) {
                        a.next = 14;
                        break;
                    }
                    if (1 != (u = i.value).orderItemFalg && 2 != u.orderItemFalg) {
                        a.next = 12;
                        break;
                    }
                    return n = !0, a.abrupt("break", 14);

                  case 12:
                    a.next = 7;
                    break;

                  case 14:
                    a.next = 19;
                    break;

                  case 16:
                    a.prev = 16, a.t0 = a.catch(5), d.e(a.t0);

                  case 19:
                    return a.prev = 19, d.f(), a.finish(19);

                  case 22:
                    p = t(s.data.orderItemVoList);
                    try {
                        for (p.s(); !(l = p.n()).done; ) "潮玩公社" == (g = l.value).ipCnName && (g.ipCnName = null), 
                        "潮玩公社" == g.ipEnName && (g.ipEnName = null);
                    } catch (e) {
                        p.e(e);
                    } finally {
                        p.f();
                    }
                    if (e.setData({
                        title: e.getOrderStatusTitle(s.data.orderStatus),
                        hasRefund: n,
                        details: s.data
                    }), 40 != e.data.details.orderStatus) {
                        a.next = 32;
                        break;
                    }
                    return a.next = 29, f.getServerTime();

                  case 29:
                    m = a.sent, console.log(m.data.systemDate), e.setData({
                        leftTime: (0, o.getRemainingDays)(e.data.details.deliveryTime, m.data.systemDate)
                    });

                  case 32:
                  case "end":
                    return a.stop();
                }
            }, r, null, [ [ 5, 16, 19, 22 ] ]);
        }))();
    },
    _sendRefresh: function(e) {
        var t = getCurrentPages();
        console.log(t), t[t.length - 2].setData({
            isRefresh: !0
        });
    },
    onReady: function() {},
    onShow: function() {
        this.getDetail();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {}
});